<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    div {
      width: 300px;
      height: 300px;
      background-color: cadetblue;
      position: absolute;
      top: 10px;
      left: 10px;
    }
  </style>

<script>
  // 拖拽
  let isDrag = false
  let x = 0
  let y = 0
  function dragstart(e) {
    isDrag = true
    x = e.clientX - box.offsetLeft
    y = e.clientY - box.offsetTop
  }
  function toggleDrag(e) {
    if (isDrag) {
      dragend(e)
    } else {
      dragstart(e)
    }
  }

  function draging(e) {
    console.log('-----');
    if (isDrag) {
      box.style.left = e.clientX - x + 'px'
      box.style.top = e.clientY - y + 'px'
    }
  }
  function dragend(e) {
    isDrag = false
  }
  document.addEventListener('mousemove', (event) => {
    draging(event)
  });
</script>
</head>
<body>
  <!-- 拖拽 -->
  <div id="box" onmousedown="dragstart(event)" onmouseup="dragend(event)" />
</body>
</html>